import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { listTeamApi, listTeamStationApi } from '../api/team'
import { listStationPipelineApi } from '../api/station'
import { getParamConfigApi, changeParamConfigApi } from '../api/paramConfig'

const teams = reactive({ arr: [] })
const stations = reactive({ arr: [] })
const pipelines = reactive({ arr: [] })
const currentTeam = ref()
const currentStation = ref()
const currentPipeline = ref()
const paramConfigForm = reactive({ val: {} })
const paramConfig = reactive({
  temperature: {},
  pressure: {},
  oilPressure: {},
  casingPressure: {},
  diffPressure: {},
  flow: {},
  totalFlow: {}
})
const editParamConfigVisible = ref(false)

async function getTeamList() {
  const { data: res } = await listTeamApi(1, 0x7fffffff)
  teams.arr = res.data.records.map((item) => {
    return {
      id: item.id,
      name: item.name
    }
  })
  currentTeam.value = teams.arr[0] ? teams.arr[0].id : null
}
async function getTeamStationList() {
  const { data: res } = await listTeamStationApi(currentTeam.value)
  stations.arr = res.data.map((item) => {
    return {
      id: item.id,
      name: item.name
    }
  })
  currentStation.value = stations.arr[0] ? stations.arr[0].id : null
}
async function getStationPipelineList() {
  const { data: res } = await listStationPipelineApi(currentStation.value)
  pipelines.arr = res.data.map((item) => {
    return {
      id: item.id,
      name: item.name
    }
  })
  currentPipeline.value = pipelines.arr[0] ? pipelines.arr[0].id : null
}
async function getParamConfig() {
  if (currentPipeline.value === null) return
  const { data: res } = await getParamConfigApi(currentPipeline.value)
  res.data.forEach((item) => {
    paramConfig[item.type] = item
  })
}
async function editParamConfig() {
  const { data: res } = await changeParamConfigApi({
    pipelineId: currentPipeline.value,
    ...paramConfigForm.val
  })
  if (!res.success) {
    ElMessage.error({
      message: res.msg,
      dangerouslyUseHTMLString: true
    })
    return
  }
  getParamConfig()
  ElMessage.success(res.data)
  editParamConfigVisible.value = false
  paramConfigForm.val = {}
}

async function initData() {
  await getTeamList()
  await getTeamStationList()
  await getStationPipelineList()
  await getParamConfig()
}
async function changeTeam() {
  await getTeamStationList()
  await getStationPipelineList()
  await changePipeline()
}
async function changeStation() {
  await getStationPipelineList()
  await changePipeline()
}
async function changePipeline() {
  for (const key in paramConfig) {
    paramConfig[key] = {}
  }
  await getParamConfig()
}

initData()

function handleChangeType() {
  const p = paramConfig[paramConfigForm.val.type]
  paramConfigForm.val.lowerLimit = p.lowerLimit
  paramConfigForm.val.upperLimit = p.upperLimit
}
function cancelEditParamConfig() {
  paramConfigForm.val = {}
  editParamConfigVisible.value = false
}

export const useParamConfig = () => {
  return {
    teams,
    stations,
    pipelines,
    currentTeam,
    currentStation,
    currentPipeline,
    paramConfigForm,
    paramConfig,
    editParamConfigVisible,
    changeTeam,
    changeStation,
    changePipeline,
    editParamConfig,
    handleChangeType,
    cancelEditParamConfig
  }
}
